package srzy.common

import scala.util.parsing.combinator._
import scala.util.matching.Regex
import sun.font.TrueTypeFont

case class AltibaseInfo(server: String, port: String, user: String, passwd: String)

object AltibaseConnStrParser extends RegexParsers {
  override def skipWhitespace = false

  def ` ` = """[ ]+""".r
  def str: Parser[String] = """[A-Za-z0-9_.]+""".r ^^ { _.toString }

  def group1: Parser[(String, String)] = "-" ~ str ~ ` ` ~ str ^^
    { case f1 ~ s1 ~ f2 ~ s2 => (s1, s2) }

  def group2: Parser[Map[String, String]] = repsep(group1, ` `) ^^
    { case s => s.toMap }

  def parse(text: String) = {
    parseAll(group2, text) match {
      case Success(r, _) => r
      case Failure(msg, _) => throw new Exception("Altibase ConnStr Parser failure :" + msg)
      case Error(msg, _) => throw new Exception("Altibase ConnStr Parser error :" + msg)
    }
  }

  def parseInfo(text: String) = {
    parseAll(group2, text) match {
      case Success(r, _) => AltibaseInfo(r.apply("s"), r.apply("port"), r.apply("u"), r.apply("p"))
      case Failure(msg, _) => throw new Exception("Altibase ConnStr Parser failure :" + msg)
      case Error(msg, _) => throw new Exception("Altibase ConnStr Parser error :" + msg)
    }
  }
}